//
//  HGIntegralVC.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/8/1.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGIntegralVC.h"
#import "HGIntegralCell.h"
#import "HGIntegralModel.h"
#import "HGGigIncomeVC.h"
#import "HGPopView.h"

@protocol HGIntegralHeaderViewDelegate <NSObject>

- (void)touchupAcrtionType:(NSInteger)type;

@end

@interface HGIntegralHeaderView : UIView

- (instancetype)initwithImage:(UIImage *)image type:(NSInteger)type;

@property (strong, nonatomic) UIButton *functionButton;

@property (strong, nonatomic) UILabel *scoreLabel;

@property (strong, nonatomic) UILabel *scoreTypeLabel;

@property (copy, nonatomic) NSArray<NSDictionary *> *buttonCofigs;

@property (weak, nonatomic) id<HGIntegralHeaderViewDelegate> delegate;
@end

@implementation HGIntegralHeaderView

- (instancetype)initwithImage:(UIImage *)image type:(NSInteger)type{
    if (self == [super init]) {
    
        UIImageView *bgImgView = [[UIImageView alloc] initWithImage:image];
        bgImgView.userInteractionEnabled = YES;
        [self addSubview:bgImgView];
        
        [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        UIColor *textColor = type == 0 ? kColorText : [UIColor whiteColor];
        
        _scoreLabel = [[UILabel alloc] init];
        _scoreLabel.text = @"00.00分";
        _scoreLabel.textColor = textColor;
        _scoreLabel.font = [UIFont systemFontOfSize:20];
        [bgImgView addSubview:_scoreLabel];
        [_scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(bgImgView);
            make.centerY.equalTo(bgImgView).multipliedBy(0.6);
        }];
        
        _scoreTypeLabel = [[UILabel alloc] init];
        _scoreTypeLabel.textColor = textColor;
        _scoreTypeLabel.text = self.buttonCofigs[type][@"promtor"];
        _scoreTypeLabel.font = [UIFont systemFontOfSize:12];
        [bgImgView addSubview:_scoreTypeLabel];
        [_scoreTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_scoreLabel.mas_bottom).offset(5);
            make.centerX.equalTo(_scoreLabel);
        }];
        
//        _functionButton = [[UIButton alloc] init];
//        _functionButton.tag = 1000 + type;
//        _functionButton.layer.cornerRadius = 15;
//        _functionButton.hidden = type != 0 ? NO : YES;
//        [_functionButton setTitle:self.buttonCofigs[type][@"name"] forState:(UIControlStateNormal)];
//        [_functionButton setTitleColor:self.buttonCofigs[type][@"color"] forState:(UIControlStateNormal)];
//        _functionButton.titleLabel.font = [UIFont systemFontOfSize:12];
//        [_functionButton setBackgroundColor:[UIColor whiteColor]];
//        [_functionButton addTarget:self action:@selector(actionCilk:) forControlEvents:(UIControlEventTouchUpInside)];
//        [bgImgView addSubview:_functionButton];
//
//        [_functionButton mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(80, 30));
//            make.right.offset(-25);
//            make.centerY.equalTo(bgImgView);
//        }];
    }
    return self;
}

- (void)actionCilk:(UIButton *)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(touchupAcrtionType:)]) {
        [_delegate touchupAcrtionType:sender.tag - 1000];
    }
}

- (NSArray<NSDictionary *> *)buttonCofigs {
    if (!_buttonCofigs) {
        _buttonCofigs = @[@{
                              @"name" : @"",
                              @"color" : [UIColor clearColor],
                              @"promtor" : @"可用灰积分"
                              },@{
                              @"name" : @"兑换惊喜",
                              @"color" : kColorWithFloat(0xea4436),
                              @"promtor" : @"可用红积分"
                              },@{
                              @"name" : @"我要挖矿",
                              @"color" : kColorWithFloat(0xfca805),
                              @"promtor" : @"可用黄积分"
                              },@{
                              @"name" : @"兑换商品",
                              @"color" : kColorWithFloat(0x4286f5),
                              @"promtor" : @"可用蓝积分"
                              }];
    }
    return _buttonCofigs;
}

@end

@interface HGIntegralVC ()<UITableViewDelegate, UITableViewDataSource, HGIntegralHeaderViewDelegate>

@property (strong, nonatomic) HGIntegralHeaderView *headerView;

@property (nonatomic,strong)UITableView* tableView;

@property (copy,nonatomic)NSMutableArray <HGIntegralModel *> *intefralModels;

@property (assign, nonatomic) NSInteger page;

@end

@implementation HGIntegralVC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerClass:[HGIntegralCell class] forCellReuseIdentifier:@"HGIntegralCell"];
    
    [self initDatas];
}

#pragma mark - 获取数据
- (void)initDatas {
    _page++;
    NSDictionary *parameter = @{
                                @"type" : [NSNumber numberWithInteger:_type + 1],
                                @"page" : [NSNumber numberWithInteger:_page]
                                };
    
    [[LMJRequestManager sharedManager] GET:KBaseURL(@"app/acct/info") parameters:parameter completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            if (_page != [response.responseModel.data[@"page"] integerValue]) {
                _tableView.mj_footer.state = MJRefreshStateNoMoreData;
                return;
            }
             NSArray *models = [HGIntegralModel mj_objectArrayWithKeyValuesArray:response.responseModel.data[@"infoVos"]];
            [self.intefralModels addObjectsFromArray:models];
            [_tableView reloadData];
            _headerView.scoreLabel.text = [NSString stringWithFormat:@"%@分",response.responseModel.data[@"balance"]];
        }
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - 用户点击不同的地方
- (void)touchupAcrtionType:(NSInteger)type {
    if (type == 2) {
        [[LMJRequestManager sharedManager] GET:KBaseURL(@"/app/acct/mine") parameters:nil completion:^(LMJBaseResponse *response) {
            if (response.responseModel.status == 1) {
                [self.navigationController pushViewController:[HGGigIncomeVC new] animated:YES];
            }
        }];
        return;
    }
    
    if (type == 1) {
        
        HGPopView *view = [[HGPopView alloc] initWithFrame:CGRectMake(0, 0, 300, 190)];
        view.closeBlock = ^{
            [LEEAlert closeWithCompletionBlock:nil];
        };
        
        [LEEAlert alert].config
        .LeeCustomView(view)
        .LeeHeaderInsets(UIEdgeInsetsMake(0, 0, 0, 0))
        .LeeClickBackgroundClose(YES)
        .LeeShow();
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _intefralModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HGIntegralCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HGIntegralCell"];
    cell.model = _intefralModels[indexPath.row];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewHeaderFooterView *hederView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"UITableViewHeaderFooterView"];
    hederView.contentView.backgroundColor = [UIColor whiteColor];
    UIButton *hederButton = [[UIButton alloc] init];
    [hederButton setTitle:@" 积分明细" forState:(UIControlStateNormal)];
    [hederButton setTitleColor:kColorDefult forState:(UIControlStateNormal)];
    hederButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [hederButton setImage:[UIImage imageNamed:@"tip"] forState:(UIControlStateNormal)];
    [hederView.contentView addSubview:hederButton];
    
    [hederButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(hederView);
        make.left.offset(15);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kColorBackground;
    [hederView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(hederView);
        make.height.offset(1);
        make.left.offset(15);
    }];
    
    return hederView;
}

- (HGIntegralHeaderView *)headerView {
    if (!_headerView) {
        NSString *imageName = [NSString stringWithFormat:@"%ld-bg",_type];
        _headerView = [[HGIntegralHeaderView alloc] initwithImage:[UIImage imageNamed:imageName] type:_type];
        _headerView.height = 100;
        _headerView.delegate = self;
    }
    return _headerView;
}

- (UITableView *)tableView {
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 40;
        _tableView.tableFooterView = [UIView new];
//        _tableView.tableHeaderView = self.headerView;
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _page = 0;
            [_intefralModels removeAllObjects];
            [self initDatas];
        }];

        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self initDatas];
        }];
        
        
        [_tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"UITableViewHeaderFooterView"];
        _tableView.sectionHeaderHeight = 40;
        [self.view addSubview:_tableView];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    return _tableView;
}

- (NSMutableArray<HGIntegralModel *> *)intefralModels {
    if (!_intefralModels) {
        _intefralModels = [@[] mutableCopy];
    }
    return _intefralModels;
}

@end
